package cn.iocoder.yudao.module.book.controller.app.homeworks;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.book.controller.app.homeworks.vo.*;
import cn.iocoder.yudao.module.book.controller.admin.homeworks.vo.HomeworksSaveReqVO;
import cn.iocoder.yudao.module.book.dal.dataobject.homeworks.HomeworksDO;
import cn.iocoder.yudao.module.book.service.homeworks.HomeworksService;
import cn.iocoder.yudao.module.book.service.users.userDTOService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.List;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import static cn.iocoder.yudao.module.book.enums.ErrorCodeConstants.HOMEWORKS_NOT_CLASS_TEACHER;

@Tag(name = "用户 App - 作业")
@RestController
@RequestMapping("/api/book/homeworks")
public class AppHomeworksController {

    @Resource
    private HomeworksService homeworksService;

    @Resource
    private userDTOService userService;

    @PostMapping("/create")
    @Operation(summary = "创建班级作业")
    public CommonResult<Integer> createHomeworks(@Valid @RequestBody AppHomeworksSaveReqVO createReqVO) {
        Long userId = userService.getCurrentUserId();
        
        // 验证用户是否为班级老师
        if (!homeworksService.isClassTeacher(createReqVO.getClassesId(), userId)) {
            throw exception(HOMEWORKS_NOT_CLASS_TEACHER);
        }
        
        // 转换为管理端的VO，因为Service接口使用的是管理端的VO
        HomeworksSaveReqVO saveReqVO = BeanUtils.toBean(createReqVO, HomeworksSaveReqVO.class);
        // 将字符串日期转换为LocalDate
        if (createReqVO.getDate() != null) {
            saveReqVO.setDate(LocalDate.parse(createReqVO.getDate(), DateTimeFormatter.ofPattern("yyyy-MM-dd")));
        }
        
        Integer homeworkId = homeworksService.createHomeworks(saveReqVO);
        return success(homeworkId);
    }

    @GetMapping("/list/{classId}")
    @Operation(summary = "获得班级作业列表")
    @Parameter(name = "classId", description = "班级ID", required = true, example = "1")
    public CommonResult<List<AppHomeworksRespVO>> getHomeworksList(@PathVariable("classId") Integer classId) {
        Long userId = userService.getCurrentUserId();
        List<AppHomeworksRespVO> homeworks = homeworksService.getClassHomeworks(classId, userId);
        return success(homeworks);
    }

    @GetMapping("/get/{id}")
    @Operation(summary = "获得作业详情")
    @Parameter(name = "id", description = "作业ID", required = true, example = "1024")
    public CommonResult<AppHomeworkDetailRespVO> getHomeworks(@PathVariable("id") Integer id) {
        AppHomeworkDetailRespVO homework = homeworksService.getHomeworkDetail(id);
        return success(homework);
    }

    @DeleteMapping("/delete/{id}")
    @Operation(summary = "删除作业")
    @Parameter(name = "id", description = "作业ID", required = true, example = "1024")
    public CommonResult<Boolean> deleteHomeworks(@PathVariable("id") Integer id) {
        Long userId = userService.getCurrentUserId();
        
        // 获取作业信息验证权限
        HomeworksDO homework = homeworksService.getHomeworks(id);
        if (!homeworksService.isClassTeacher(homework.getClassesId(), userId)) {
            throw exception(HOMEWORKS_NOT_CLASS_TEACHER);
        }
        
        homeworksService.deleteHomeworks(id);
        return success(true);
    }
}
